package com.serkol.utils;


import com.alibaba.fastjson2.JSONObject;
import com.serkol.ams.entity.LoginLog;
import com.serkol.constant.AuthConstant;
import com.serkol.utils.sm.SM4Utils;
import org.apache.commons.lang3.time.DateUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;


public class TokenUtils {


    public static LoginLog getAuth(String token) {
        SM4Utils sm4Utils = new SM4Utils();
        String s = sm4Utils.decryptData_ECB(token);
     //   System.out.println(s);
        LoginLog login = JSONObject.parseObject(s, LoginLog.class);
        if (login != null) {
            System.out.println(login);
            login.setTerminal(login.getTerminal()==null ? 0 : login.getTerminal());
            if(login.getTerminal()==1) {
                Date createDate = login.getCreateDate();
                if (createDate == null) {
                    return null;
                }
                // System.out.println(createDate);
                Date date = DateUtils.addHours(createDate, 2);
                Date date1 = new Date();
                if (date.getTime() < date1.getTime()) {
                    return null;
                }
            }
            String s1 = "add,view,user,edit";
            String[] split = s1.split(",");
            List<String> strings = Arrays.asList(split);
            AuthConstant.setAuth(strings);
            AuthConstant.setLoginLog(login);
          //  System.out.println(login);
            return login;
        } else {
            return null;
        }

        // return login;
    }


}
